/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.soap.mail.message.CheckSpellingResponse;
/*     */ import com.zimbra.soap.mail.type.Misspelling;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSpellCheck
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String TEXT = "On a cycle the fram is gone. You're completly in cotnact with it all.\nYou're in the scene, not just watching it anmore, and the sense of presence\nis overwhelming. That concret whizing by five inches below your foot is the\nreal thing, the same \"stuff\" you walk on, it's right there, so blurred you can't\nfocus on it, yet you can put your foot down and touch it anytime, and the\nwhole thing, the whole experience, is nevr removed from immediate\nconsciousness.";
/*     */   private String[] originalDictionaries;
/*  55 */   private boolean available = false;
/*     */   private String[] originalAccountIgnoreWords;
/*     */   private String[] originalDomainIgnoreWords;
/*     */   private String[] originalCosIgnoreWords;
/*     */   private String originalIgnoreAllCaps;
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  63 */     Provisioning prov = Provisioning.getInstance();
/*  64 */     this.originalDictionaries = prov.getLocalServer().getSpellAvailableDictionary();
/*     */     
/*  66 */     Account account = TestUtil.getAccount("user1");
/*  67 */     this.originalAccountIgnoreWords = account.getPrefSpellIgnoreWord();
/*  68 */     this.originalDomainIgnoreWords = prov.getDomain(account).getPrefSpellIgnoreWord();
/*  69 */     this.originalCosIgnoreWords = prov.getCOS(account).getPrefSpellIgnoreWord();
/*  70 */     this.originalIgnoreAllCaps = account.getAttr("zimbraPrefSpellIgnoreAllCaps");
/*     */     
/*  72 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  73 */     CheckSpellingResponse result = mbox.checkSpelling("test");
/*  74 */     this.available = result.isAvailable();
/*  75 */     if (!this.available) {
/*  76 */       ZimbraLog.test.info("Spell checking service is not available.  Skipping tests.");
/*     */     }
/*     */   }
/*     */   
/*     */   public void testCheckSpelling() throws Exception
/*     */   {
/*  82 */     if (!this.available) {
/*  83 */       return;
/*     */     }
/*     */     
/*     */ 
/*  87 */     Account account = TestUtil.getAccount("user1");
/*  88 */     account.setPrefSpellIgnoreWord(new String[] { "completly" });
/*  89 */     Provisioning prov = Provisioning.getInstance();
/*  90 */     prov.getDomain(account).setPrefSpellIgnoreWord(new String[] { "anmore" });
/*  91 */     prov.getCOS(account).setPrefSpellIgnoreWord(new String[] { "concret" });
/*     */     
/*  93 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  94 */     CheckSpellingResponse result = mbox.checkSpelling("On a cycle the fram is gone. You're completly in cotnact with it all.\nYou're in the scene, not just watching it anmore, and the sense of presence\nis overwhelming. That concret whizing by five inches below your foot is the\nreal thing, the same \"stuff\" you walk on, it's right there, so blurred you can't\nfocus on it, yet you can put your foot down and touch it anytime, and the\nwhole thing, the whole experience, is nevr removed from immediate\nconsciousness.");
/*  95 */     assertTrue(result.isAvailable());
/*     */     
/*     */ 
/*  98 */     Map<String, List<String>> map = new HashMap();
/*  99 */     for (Misspelling mis : result.getMisspelledWords()) {
/* 100 */       map.put(mis.getWord(), mis.getSuggestionsList());
/*     */     }
/*     */     
/* 103 */     assertEquals("Number of misspelled words", 4, getNumMisspellings(result));
/* 104 */     assertTrue("fram", hasSuggestion(result, "fram", "from"));
/* 105 */     assertTrue("cotnact", hasSuggestion(result, "cotnact", "contact"));
/* 106 */     assertTrue("whizing", hasSuggestion(result, "whizing", "whizzing"));
/* 107 */     assertTrue("nevr", hasSuggestion(result, "nevr", "never"));
/* 108 */     ZimbraLog.test.debug("Successfully tested spell checking");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testIgnore()
/*     */     throws Exception
/*     */   {
/* 116 */     if (!this.available) {
/* 117 */       return;
/*     */     }
/*     */     
/* 120 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 121 */     CheckSpellingResponse result = mbox.checkSpelling("one twi thre forr", null, Arrays.asList(new String[] { "twi", "thre" }));
/* 122 */     assertEquals("Number of misspelled words", 1, getNumMisspellings(result));
/* 123 */     assertTrue(hasSuggestion(result, "forr", "four"));
/*     */   }
/*     */   
/*     */   private int getNumMisspellings(CheckSpellingResponse result) {
/* 127 */     return result.getMisspelledWords().size();
/*     */   }
/*     */   
/*     */   private boolean hasSuggestion(CheckSpellingResponse result, String misspelled, String expectedSuggestion) {
/* 131 */     for (Misspelling mis : result.getMisspelledWords()) {
/* 132 */       if (mis.getWord().equals(misspelled)) {
/* 133 */         for (String suggestion : mis.getSuggestionsList()) {
/* 134 */           if (suggestion.equals(expectedSuggestion)) {
/* 135 */             return true;
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 140 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testGetDictionaries()
/*     */     throws Exception
/*     */   {
/* 149 */     Server server = Provisioning.getInstance().getLocalServer();
/* 150 */     server.setSpellAvailableDictionary(new String[] { "dict1", "dict2" });
/* 151 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 152 */     Element request = new Element.XMLElement(MailConstants.GET_SPELL_DICTIONARIES_REQUEST);
/* 153 */     Element response = mbox.invoke(request);
/*     */     
/*     */ 
/* 156 */     Set<String> expected = new HashSet();
/* 157 */     expected.add("dict1");
/* 158 */     expected.add("dict2");
/*     */     
/* 160 */     Set<String> actual = new HashSet();
/* 161 */     for (Element eDict : response.listElements("dictionary")) {
/* 162 */       actual.add(eDict.getText());
/*     */     }
/*     */     
/* 165 */     assertEquals(2, actual.size());
/* 166 */     actual.removeAll(expected);
/* 167 */     assertEquals(0, actual.size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testUnexpectedCharacters()
/*     */     throws Exception
/*     */   {
/* 175 */     if (!this.available) {
/* 176 */       return;
/*     */     }
/*     */     
/*     */ 
/* 180 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 181 */     CheckSpellingResponse result = mbox.checkSpelling("one  tuo two");
/* 182 */     assertEquals(1, result.getMisspelledWords().size());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testReceiveSpanish()
/*     */     throws Exception
/*     */   {
/* 190 */     if (!this.available) {
/* 191 */       return;
/*     */     }
/*     */     
/* 194 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 195 */     CheckSpellingResponse result = mbox.checkSpelling("reunion", "es");
/* 196 */     assertEquals(1, result.getMisspelledWords().size());
/* 197 */     Misspelling misspelling = (Misspelling)result.getMisspelledWords().get(0);
/* 198 */     assertEquals("reunion", misspelling.getWord());
/* 199 */     assertTrue(misspelling.getSuggestionsList().contains("reunión"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testSendSpanish()
/*     */     throws Exception
/*     */   {
/* 207 */     if (!this.available) {
/* 208 */       return;
/*     */     }
/*     */     
/* 211 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 212 */     CheckSpellingResponse result = mbox.checkSpelling("últimos esstá", "es");
/* 213 */     assertEquals(1, result.getMisspelledWords().size());
/* 214 */     Misspelling misspelling = (Misspelling)result.getMisspelledWords().get(0);
/* 215 */     assertEquals("esstá", misspelling.getWord());
/* 216 */     assertTrue(misspelling.getSuggestionsList().contains("está"));
/*     */   }
/*     */   
/*     */   public void testRussian() throws Exception
/*     */   {
/* 221 */     if (!this.available) {
/* 222 */       return;
/*     */     }
/*     */     
/* 225 */     String krokodil = "Крокодил";
/* 226 */     String krokodilMisspelled = "Крокодилл";
/* 227 */     String cherepaha = "Черепаха";
/*     */     
/* 229 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 230 */     CheckSpellingResponse result = mbox.checkSpelling(krokodilMisspelled + " " + cherepaha, "ru");
/* 231 */     assertEquals(1, result.getMisspelledWords().size());
/* 232 */     Misspelling misspelling = (Misspelling)result.getMisspelledWords().get(0);
/* 233 */     assertEquals(krokodilMisspelled, misspelling.getWord());
/* 234 */     assertTrue(misspelling.getSuggestionsList().contains(krokodil));
/*     */   }
/*     */   
/*     */   public void testAllCaps() throws Exception
/*     */   {
/* 239 */     if (!this.available) {
/* 240 */       return;
/*     */     }
/*     */     
/* 243 */     Account account = TestUtil.getAccount("user1");
/* 244 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/* 246 */     account.setPrefSpellIgnoreAllCaps(false);
/* 247 */     CheckSpellingResponse result = mbox.checkSpelling("XYZ");
/* 248 */     assertEquals(1, result.getMisspelledWords().size());
/*     */     
/* 250 */     account.setPrefSpellIgnoreAllCaps(true);
/* 251 */     result = mbox.checkSpelling("XYZ");
/* 252 */     assertEquals(0, result.getMisspelledWords().size());
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 257 */     Provisioning prov = Provisioning.getInstance();
/* 258 */     prov.getLocalServer().setSpellAvailableDictionary(this.originalDictionaries);
/*     */     
/* 260 */     Account account = TestUtil.getAccount("user1");
/* 261 */     account.setPrefSpellIgnoreWord(this.originalAccountIgnoreWords);
/* 262 */     prov.getDomain(account).setPrefSpellIgnoreWord(this.originalDomainIgnoreWords);
/* 263 */     prov.getCOS(account).setPrefSpellIgnoreWord(this.originalCosIgnoreWords);
/* 264 */     TestUtil.setAccountAttr("user1", "zimbraPrefSpellIgnoreAllCaps", this.originalIgnoreAllCaps);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 269 */     TestUtil.cliSetup();
/* 270 */     TestUtil.runTest(TestSpellCheck.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSpellCheck.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */